//
//  UITableView+Custom.m
//  OMS
//
//  Created by goldeney on 14/11/4.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "UITableView+Custom.h"
#import "RTSpinKitView.h"

@implementation UITableView (Custom)

#pragma mark
#pragma mark - 错误提示页面

- (void)hideProgress
{
    self.tableFooterView = [[UIView alloc] init];
    [self reloadData];
}

- (void)showTableViewProgress
{
    [self showCustomProgressOffset:0];
}

- (void)showCustomProgressOffset:(CGFloat)offset
{
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    CGFloat height = [[UIScreen mainScreen] bounds].size.height;
    
    if (self.bounds.size.width < width) width = self.bounds.size.width;
    if (self.bounds.size.height < height) height = self.bounds.size.height;
    
    CGFloat headHeight = self.tableHeaderView.frame.size.height;
    height -= headHeight;
    
    CGRect viewFrame = CGRectMake(0, 0, width, height- offset);
    UIView *view = [[UIView alloc] initWithFrame:viewFrame];
    
    RTSpinKitView *spinner = [[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleFadingCircleAlt];
    spinner.center = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));
    [spinner startAnimating];
    [view addSubview:spinner];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, viewFrame.size.width, 26)];
    lable.center = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds) + 43);
    lable.textAlignment = NSTextAlignmentCenter;
    lable.font = [UIFont systemFontOfSize:13];
    lable.textColor = [UIColor grayColor];
    lable.text = @"努力加载中";
    [view addSubview:lable];
    
    self.tableFooterView = view;
    [self reloadData];
}

- (void)showTableViewProgressOffset:(CGFloat)offset
{
    CGRect viewFrame = CGRectMake(0, 0, SuperWidth, self.bounds.size.height-self.tableHeaderView.frame.size.height - offset);
    UIView *view = [[UIView alloc] initWithFrame:viewFrame];
    
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:
                                                  UIActivityIndicatorViewStyleGray];
    activityIndicator.center = CGPointMake(viewFrame.size.width/2, viewFrame.size.height/2);
    [activityIndicator startAnimating];
    [activityIndicator setHidesWhenStopped:YES];
    [view addSubview:activityIndicator];
    
    self.tableFooterView = view;
}

- (void)setTableViewNotDataDrawText:(NSString*)text isShow:(BOOL)isShow
{
    [self setTableViewNotDataDrawText:text isShow:isShow offset:0];
}

- (void)setTableViewNotDataDrawText:(NSString*)text isShow:(BOOL)isShow offset:(CGFloat)offset
{
    if (isShow) {
        CGFloat width = [[UIScreen mainScreen] bounds].size.width;
        CGFloat height = [[UIScreen mainScreen] bounds].size.height;
        
        if (self.bounds.size.width < width) width = self.bounds.size.width;
        if (self.bounds.size.height < height) height = self.bounds.size.height;
        
        CGFloat headHeight = self.tableHeaderView.frame.size.height;
        height -= headHeight;
        
        CGRect viewFrame = CGRectMake(0, 0, width, height - offset);
        UIView *view = [[UIView alloc] initWithFrame:viewFrame];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guess_like_img"]];
        CGFloat imageViewWidth = imageView.frame.size.width;
        CGFloat imageViewHeight = imageView.frame.size.height;
        imageView.frame = CGRectMake(view.frame.size.width/2 - imageViewWidth/2, view.frame.size.height/2 - imageViewHeight/2 - 44,imageViewWidth,imageViewHeight);
        [view addSubview:imageView];
        
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.origin.x - imageViewWidth/2, imageView.frame.origin.y + imageViewHeight + 4, imageViewWidth * 2, 26)];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.font = [UIFont systemFontOfSize:15];
        lable.textColor = [UIColor grayColor];
        lable.text = text;
        [view addSubview:lable];
        self.tableFooterView = view;
    } else {
        self.tableFooterView = [[UIView alloc] init];
    }
    [self reloadData];
}

- (void)setTableViewDrawText:(NSString*)text isShow:(BOOL)isShow
{
    if (isShow) {
        CGFloat width = [[UIScreen mainScreen] bounds].size.width;
        CGRect viewFrame = CGRectMake(0, 0, width, 26);
        UIView *view = [[UIView alloc] initWithFrame:viewFrame];
        
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, viewFrame.size.width, 26)];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.font = [UIFont systemFontOfSize:16];
        lable.textColor = [UIColor grayColor];
        lable.text = text;
        [view addSubview:lable];
        self.tableFooterView = view;
    } else {
        self.tableFooterView = [[UIView alloc] init];
    }
    [self reloadData];
}

- (void)setTableViewActivityIndicatorViewIsShow:(BOOL)isShow
{
    if (isShow) {
        CGFloat width = [[UIScreen mainScreen] bounds].size.width;
        CGFloat height = [[UIScreen mainScreen] bounds].size.height;
        
        if (self.bounds.size.width < width) width = self.bounds.size.width;
        if (self.bounds.size.height < height) height = self.bounds.size.height;
        
        CGFloat headHeight = self.tableHeaderView.frame.size.height;
        height -= headHeight;
        
        CGRect viewFrame = CGRectMake(0, 0, width, height);
        UIView *view = [[UIView alloc] initWithFrame:viewFrame];
        
        UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] init];
        [activity setCenter:CGPointMake(width/2, height/2)];
        [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        activity.hidesWhenStopped = YES;
        [activity startAnimating];
        
        [view addSubview:activity];
        self.tableFooterView = view;
    } else {
        self.tableFooterView = [[UIView alloc] init];
    }
    [self reloadData];
}

@end
